package com.gree.scmservice.dao;

import com.gree.common.entity.SysDictData;
import com.gree.common.entity.SysDictType;
import com.gree.common.entity.TablePageData;
import com.gree.scmservice.entity.saas.SaasSysAddon;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class SysDictDataDao {

    @Autowired
    private Dao dao;

    public List<SysDictData> selectDictDataByType(String dictType){
        Sql sql = Sqls.create("select dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, remark " +
                " from sys_dict_data where status = '0' and dict_type = @dictType order by dict_sort asc ");
        sql.setParam("dictType",dictType);
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(SysDictData.class));
        List<SysDictData> dataList=dao.execute(sql).getList(SysDictData.class);
        return dataList;
    }

    public TablePageData<SysDictData> selectDictDataList(SysDictData sysDictData){
        Pager pager = dao.createPager(sysDictData.getPageNum(), sysDictData.getPageSize());
        sysDictData.setBaseEntityNull();
        Cnd cnd = Cnd.from(dao, sysDictData);
        List query = dao.query(SysDictData.class, cnd, pager);
        pager.setRecordCount(dao.count(SysDictData.class, cnd));
        TablePageData tablePageData = new TablePageData(query, pager);
        return tablePageData;
    }

    public List<Record> selectDictTypeAll(){
        Sql sql = Sqls.create("select dict_id, dict_name, dict_type, status, create_by, create_time, remark from sys_dict_type");
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(Record.class));
        List<Record> dataList=dao.execute(sql).getList(Record.class);
        return dataList;
    }

    public int countDictDataByType(String dictType){
        return dao.count(SysDictData.class, Cnd.where("dict_type","=",dictType));
    }

    public int updateDictDataType(String oldDictType, String newDictType) {
        int result = dao.update(SysDictType.class, Chain.make("dict_type",newDictType),Cnd.where("dict_type","=",oldDictType));
        return result;
    }

    public List<SysDictData> querySysDictData(Cnd cnd, Pager pager) {
        List<SysDictData> sysDictDataList = dao.query(SysDictData.class,cnd,pager);
        return sysDictDataList;
    }

    public SysDictData fetchSysDictById(Long dictCode) {
        SysDictData sysDictData = dao.fetch(SysDictData.class,dictCode);
        return sysDictData;
    }

    public int deleteSysDictDataByDictCodes(Long[] dictCodes) {
        int result = dao.clear(SysDictData.class,Cnd.where("dict_code","in",dictCodes));
        return result;
    }

    public SysDictData insertSysDictData(SysDictData sysDictData) {
        SysDictData result = dao.insert(sysDictData);
        return result;
    }

    public int updateSysDictData(SysDictData sysDictData) {
        int result = dao.update(sysDictData);
        return result;
    }
}
